<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&lt;?php
namespace Library\Dal\Adapter;

class Mysql implements \Library\Dal\AdapterInterface
{
    protected $_resource;
    protected $_settings;
    protected $_parameters = array();
    
    public $isConnected = false;
    
    public function __construct( $settings )
    {
        $this-&gt;_settings = $settings;
    }
    
    public function getConnection()
    {
        if ( ! $this-&gt;_resource instanceof \PDO )
        {
            $this-&gt;connect();
        }
        
        return $this-&gt;_resource;
    }
    
    public function connect()
    {
        if ( is_array( $this-&gt;_settings ) )
        {
            $this-&gt;_resource = new \PDO( &quot;mysql:dbname=&quot; . $this-&gt;_settings[&quot;database&quot;] . &quot;;host=&quot; . $this-&gt;_settings[&quot;server&quot;], 
                                         $this-&gt;_settings[&quot;username&quot;], 
                                         $this-&gt;_settings[&quot;password&quot;], 
                                         array(
                                             \PDO::ATTR_PERSISTENT =&gt; true,
                                             \PDO::ATTR_ERRMODE =&gt; \PDO::ERRMODE_EXCEPTION
                                         ) 
                                     );
            
            if ( $this-&gt;_resource !== false )
            {
                $this-&gt;isConnected = true;
            }
        }
        else
        {
            throw new \Exception( &quot;Not implemented.&quot; );
        }
    }
    
    public function getAttr( $key )
    {
        return $this-&gt;_resource-&gt;getAttribute( $key );
    }
    
    public function setAttr( $key, $value )
    {
        return $this-&gt;_resource-&gt;setAttribute( $key, $value );
    }
    
    public function createParameter( $key, $value, $type = DataType::String )
    {
        $this-&gt;_parameters[] = array( &quot;:&quot; . $key, $value, $type );
    }
    
    public function prepare( $query )
    {
        $st = $this-&gt;getConnection()-&gt;prepare( $query );
        
        foreach( $this-&gt;_parameters as $parameter )
        {
            $st-&gt;bindParam( $parameter['0'], $parameter['1'], $parameter['2'] );
        }
        
        $this-&gt;_parameters = array();
        
        return $st;
    }

    public function query( $query )
    {
        $st = $this-&gt;prepare( $query );
        
        $st-&gt;execute();
        
        return $st;
    }

    public function fetchAll( $query )
    {
        return $this-&gt;Query( $query )-&gt;fetchAll( \PDO::FETCH_ASSOC );
    }

    public function fetchRow( $query )
    {
        return $this-&gt;Query( $query )-&gt;fetch( \PDO::FETCH_ASSOC );
    }

    public function countRows( $query )
    {
        
    }
    
    public function close()
    {
        $this-&gt;_resource = null;
    }
}</pre>
</body>
</html>
